package edu.whu.demo.controller;

import edu.whu.demo.entity.Commodity;
import edu.whu.demo.entity.Supplier;
import edu.whu.demo.service.CommodityService;
import edu.whu.demo.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ServletResponseMethodArgumentResolver;

import javax.transaction.RollbackException;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private SupplierService supplierService;

    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getCommodity(@PathVariable long id) {
        Commodity commodity = commodityService.getCommodity(id);
        if(commodity == null) {
            return ResponseEntity.noContent().build();
        }
        else{
            return ResponseEntity.ok(commodity);
        }
    }

    @GetMapping("/supplier/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id) {
        Supplier supplier = supplierService.getSupplier(id);
        if(supplier == null) {
            return ResponseEntity.noContent().build();
        }
        else {
            return ResponseEntity.ok(supplier);
        }
    }

//    @GetMapping("/{id}/supplier")
//    public ResponseEntity<List<Supplier>> getSuppliersByID(@PathVariable Long id) {
//
//    }

    @PostMapping("")
    @Transactional(rollbackOn = Exception.class)
    public ResponseEntity<String> addCommodity(@RequestBody Commodity commodity) {
        try{
            commodity.getSuppliers().forEach(supplier -> {
                Supplier result = supplierService.addSupplier(supplier);
                supplier.setId(result.getId());

            });
            Commodity result = commodityService.addCommodity(commodity);
            return ResponseEntity.ok("add commodity"+result.getId());
        }
        catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/supplier")
    @Transactional
    public ResponseEntity<String> addSupplier(@RequestBody Supplier supplier) {
        try{
            Supplier result = supplierService.addSupplier(supplier);
            return ResponseEntity.ok("add supplier" + result.getId());
        }
        catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateCommodity(@PathVariable long id, @RequestBody Commodity commodity) {
        commodity.getSuppliers().forEach(supplier -> {
            if(supplierService.getSupplier(supplier.getId()) == null){
                Supplier result = supplierService.addSupplier(supplier);
                supplier.setId(result.getId());
            }
            else{
                supplierService.updateSupplier(supplier.getId(), supplier);
            }
        });
        commodityService.updateCommodity(id, commodity);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/supplier/{id}")
    public ResponseEntity<Void> updateSupplier(@PathVariable long id, @RequestBody Supplier supplier){
        supplierService.updateSupplier(id, supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCommodity(@PathVariable long id) {
        commodityService.deleteCommodity(id);
        //commodityService.deleteRecordByCommodityId(id);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/supplier/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable long id) {
        try{
            //supplierService.deleteRecordBySupplierId(id);
            supplierService.deleteSupplier(id);
            return ResponseEntity.ok().build();
        }
        catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }



    }

    @GetMapping("")
    public ResponseEntity<List<Commodity>> findCommodities(String name, Double price) {
        List<Commodity> list = commodityService.findCommodities(name, price);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/supplier_commodity/{id}")
    public ResponseEntity<List<Commodity>> findCommoditiesBySupplier(@PathVariable long id) {
        List<Commodity> list = commodityService.findCommoditiesBySupplier(id);
        if(list.size() == 0){
            return ResponseEntity.ok().build();
        }
        else{
            return ResponseEntity.ok(list);
        }
    }

}
